#include "..\headers\ListOf.h"

typedef struct LNodeList {
  int data;
  struct LNodeList *next;
} LNodeList, *LinkList;

void freeList(LinkList head) {
  LinkList temp;
  while (head) {
    temp = head;
    head = head->next;
    free(temp);
  }
}
LinkList CreatList() {
  LinkList Node = NULL, p = NULL, p1 = NULL;
  int f;
  while (1) {
    scanf("%d", &f);
    if (f == -1) break;
    p1 = (LinkList)malloc(sizeof(LNodeList));
    if (p1 == NULL) {
      printf("Memory allocation failed. Exiting...\n");
      freeList(Node);
      return NULL;
    }
    p1->data = f;
    p1->next = NULL;
    if (Node == NULL)
      Node = p1;
    else
      p->next = p1;
    p = p1;
  }
  return Node;
}

int main() {
  LinkList head1, head2, p1, p2, p3, head3 = NULL;
  head1 = CreatList();
  head2 = CreatList();
  if (!head1 && !head2) {
    printf("NULL");
    return 0;
  }
  if (head1 != NULL && head2 == NULL) {
    while (head1) {
      printf("%d", head1->data);
      if (head1->next != NULL) printf(" ");
      head1 = head1->next;
    }
    return 0;
  }
  if (head1 == NULL && head2 != NULL) {
    while (head2) {
      printf("%d", head2->data);
      if (head2->next != NULL) printf(" ");
      head2 = head2->next;
    }
    return 0;
  }

  p1 = head1, p2 = head2;
  p3 = (LinkList)malloc(sizeof(LNodeList));
  while (p1 && p2) {
    if (p1->data > p2->data) {
      p3->next = p2;
      p3 = p2;
      p2 = p2->next;
    } else {
      p3->next = p1;
      p3 = p1;
      p1 = p1->next;
    }
    if (head3 == NULL) head3 = p3;
  }
  if (p1 != NULL && !p2)
    p3->next = p1;
  else
    p3->next = p2;
  while (head3) {
    printf("%d", head3->data);
    if (head3->next != NULL) printf(" ");
    head3 = head3->next;
  }

  freeList(p1);
  freeList(p2);
  free(p3);
  return 0;
}
